package dao;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.sun.org.apache.xpath.internal.operations.Bool;

import utilClasses.ListaString;
import utilClasses.ListaUser;
import utilClasses.uLogin;



public class daoKorisnik {

	public static ListaUser dajSveUsere() throws SQLException{
	Connection c = daoKonekcija.dajKonekciju();
	ListaUser rez=new ListaUser();
	String iskaz = "SELECT iduserLogin FROM userlogin";
	try {
		Statement stmt = c.createStatement();
		ResultSet rs=stmt.executeQuery(iskaz);
		while (rs.next()) {
	    	String korisnickoIme=rs.getString(1);
	        rez.getUsers().add(new uLogin(korisnickoIme)); 
	        }
	    rs.close();
	    stmt.close();
	   
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		
		e.printStackTrace();
	}finally{
		c.close();
	}
	
	return rez;
	}
	public static ListaUser dajUsereLike(String poredi) throws SQLException{
		ListaUser rez=new ListaUser();
		Connection c = daoKonekcija.dajKonekciju();
		String iskaz = "SELECT iduserLogin FROM userlogin where iduserLogin like ?";
		try {
			PreparedStatement pstmt = c.prepareStatement(iskaz);
			pstmt.setString(1,poredi);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				String korisnickoIme=rs.getString(1);
			    rez.getUsers().add(new uLogin(korisnickoIme)); 
			    }
			rs.close();
			pstmt.close();
			c.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}finally{
				c.close();
			}
		return rez;
	}
	static void provjeraPermisije(String imePrezime) throws SQLException { 
	Connection c = daoKonekcija.dajKonekciju();
	CallableStatement cStmt = c.prepareCall("{?=CALL PermisijaKorisnik(?)}"); 
	cStmt.setString(1,imePrezime); 
	cStmt.execute(); 
	//System.out.println("Uspjesno"); 
	String permisija= cStmt.getString(1);
	System.out.println("Permisije su: "+ permisija);
	cStmt.close(); 
	c.close();
	}

	public static Boolean ProvjeriHash (String username, String password) throws SQLException{
	Connection c = daoKonekcija.dajKonekciju();
	boolean jel=false;
		try {
	   String iskaz = "SELECT iduserLogin, password FROM userlogin where iduserLogin=?";
	   PreparedStatement pstmt = c.prepareStatement(iskaz);
	   pstmt.setString(1, username);
	  
	   ResultSet rs = pstmt.executeQuery();
	    
	    while (rs.next()) {
	    	String korisnickoIme=rs.getString(1);
	        String hP = rs.getString(2);
	        
	        if(korisnickoIme.equals(username) && hP.equals(daoKorisnik.md5(password))){
	        	System.out.println("Isti");
	        	jel= true; 
	        }
	    }
	    rs.close();
	    pstmt.close();
	    
	 }
	    catch (SQLException e)
	    {
	      System.err.println("Greska");
	    }finally{
	    	c.close();
	    }
	System.out.println("Nisu isti");
	return jel;	
	}

	static void insertUserLogin( String iduserLogin, String password, int korisnik) throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
	CallableStatement cStmt = c.prepareCall("{CALL Insert_userLogin(?,?,?)}");
	cStmt.setString(1, iduserLogin);
	cStmt.setString(2, daoKorisnik.md5(password));
	cStmt.setInt(3, korisnik);
	
	cStmt.execute();
	
	System.out.println("Uspjesno uneseni korisnicki podaci!\n");
	cStmt.close();
	c.close();
	}

	static void insertKorisnik( String ImePrezime, String Telefon, int tip) throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
	CallableStatement cStmt = c.prepareCall("{CALL Insert_korisnik(?,?,?)}");
	cStmt.setString(1, ImePrezime);
	cStmt.setString(2, Telefon);
	cStmt.setInt(3, tip);
	
	cStmt.execute();
	
	System.out.println("Uspjesno unesen korisnik!\n");
	cStmt.close();
	c.close();
	}

	public static void deleteTipKorisnika ( int id ) 
		throws SQLException  { 
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c.prepareCall("{CALL deleteTipKorisnika(?)}");
	
		cStmt.setInt(1, id);
		cStmt.executeUpdate();
		System.out.println("Uspjesno obrisan korisnik!\n");
		cStmt.close();
		c.close();
	}

	public static void updateTipKorisnika ( int id, String nazivTipaKorisnika)
	    throws SQLException  { 
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c.prepareCall("{CALL updateTipKorisnika(?,?)}");
	
	    cStmt.setInt(1, id);
	    cStmt.setString(2, nazivTipaKorisnika);
	
		cStmt.executeUpdate();
	System.out.println("Uspjesno promijenjen parametar!\n");
	cStmt.close();
	c.close();
	}

	static void insertTipKorisnika( String nazivTipaKorisnika) throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
	CallableStatement cStmt = c.prepareCall("{CALL Insert_tipKorisnika(?)}");
	cStmt.setString(1, nazivTipaKorisnika);
	
	cStmt.execute();
	
	System.out.println("Uspjesno unesen korisnik!\n");
	cStmt.close();
	c.close();
	}

	public static String md5(String input) {
	    
	    String md5 = null;
	     
	    if(null == input) return null;
	     
	    try {
	         
	    //Create MessageDigest object for MD5
	    MessageDigest digest = MessageDigest.getInstance("MD5");
	     
	    //Update input string in message digest
	    digest.update(input.getBytes(), 0, input.length());
	
	    //Converts message digest value in base 16 (hex) 
	    md5 = new BigInteger(1, digest.digest()).toString(16);
	
	    } catch (NoSuchAlgorithmException e) {
	
	        e.printStackTrace();
	    }
	    return md5;
	}
	public static ListaString dajTipoveKorisnika() throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		ListaString rez=new ListaString();
		String iskaz = "SELECT nazivTipaKorisnika FROM tipkorisnika";
		try {
			Statement stmt = c.createStatement();
			ResultSet rs=stmt.executeQuery(iskaz);
			while (rs.next()) {
		    	String naziv=rs.getString(1);
		        rez.getVrijednost().add(new String(naziv)); 
		        }
			rs.close();
		    stmt.close();
		    c.close();
		    
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}finally{
			c.close();
		}
		return rez;
	}
	public static String RegistrujKorisnika(String ime, String telefon, String tip, String uname, String pass) throws SQLException{
		String rez=new String("true");
		Connection c = daoKonekcija.dajKonekciju();
		try {
			CallableStatement cStmt1 = c.prepareCall("{CALL dajIdTipaKorisnika (?)}"); 
			cStmt1.setString(1,tip);
			ResultSet rs = cStmt1.executeQuery(); ;
			int ID=0;
			if(rs.next()) {
				ID=rs.getInt(1);
			}
			rs.close();
			cStmt1.close();
			CallableStatement cStmt2 = c.prepareCall("{CALL registrujKorisnika(?,?,?,?,?)}");
			cStmt2.setString(1, ime);
			cStmt2.setString(2, telefon);
			cStmt2.setInt(3, ID);
			cStmt2.setString(4, uname);
			cStmt2.setString(5, pass);
			cStmt2.execute();
			cStmt2.close();
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			rez = new String("false");
		}finally{
			c.close();
		}
		return rez;
		
	}
	

}
